<template>
	<div>
		<div class="poster" :style="'background: ' + hdInfo.color + ' url(' + hdInfo.img + ') center top / 100%  no-repeat'">
			<div class="btn" @click="hdyy"> <image :src="hdInfo.btn" mode="widthFix"></image> </div>
		</div>
	</div>
</template>

<script>
import { mapActions } from 'vuex';
const db = wx.cloud.database();
export default {
	data() {
		return {
			hdInfo: {
				color: "#480001",
				img: "https://www.baixuemianshi.com/zhuanti/xcx/ggyy/poster.jpg",
				btn: "https://www.baixuemianshi.com/zhuanti/images/btn_cjyy.png"
			},
			name: "",
			templateId: "vZSd8u06ZovsLBT9-4IFw47gegiReJDTelb8TISZ-Kk",
			openid: "",
			yycount: 0
		};
	},

	onShow() {},
	onPullDownRefresh() {
		console.log('refresh');
		setTimeout(function() {
			uni.stopPullDownRefresh();
		}, 1000);
	},
	onPageScroll(e) {
		let that = this;
		that.scrollTop = e.scrollTop;
	},
	onLoad(options) {
		if (!options) {
		} else {
			this.name = options.name;
		}
	},
	computed: {
		userphone() {
			return this.$store.getters.userphone ? this.$store.getters.userphone : '';
		},
		useropenid() {
			return this.$store.getters.useropenid ? this.$store.getters.useropenid : '';
		},
		username() {
			return this.$store.getters.username ? this.$store.getters.username : '';
		},
		loginpage() {
			return this.$store.getters.loginpage ? this.$store.getters.loginpage : '';
		},
		favzw() {
			return this.$store.getters.favzw ? this.$store.getters.favzw : '';
		}
	},
	mounted() {
		this.name = this.name ? this.name : this.username;
		this.LogSetName(this.name);
		
	},
	watch: {
		
	},
	created() {
		let that = this;
		that.getopenid();
	},
	methods: {
		...mapActions(['LogSetSearchtimes', 'LogSetPhone', 'LogSetOpenid', 'LogSetName', 'LogSetPage', 'LogSetFavzw']),
		getopenid() {
			let that = this;
			wx.cloud.callFunction({
				name: 'getopenid',
				data: {},
				complete: res => {
					console.log(res)
					that.openid = res.result.openid
					that.yyStatus()
				}
			});
		},
		toLogin() {
			let path = '../../my/login?navTlt=' + '省考公告预约';
			uni.navigateTo({
				url: path
			});
		},
		async yyStatus(){
			let count = await db.collection('24sk_ggyy').where({_openid: this.openid}).count()
			this.yycount = count.total
			if(count.total > 0){
				this.changeWeb("https://www.baixuemianshi.com/zhuanti/sk_stl/")
				return
			}
		},
		hdyy() {
			let that = this
			if(that.yycount > 0){
				uni.showToast({
					title:"您已预约",
					icon:"success",
					duration: 1000,
					success() {
						that.changeWeb("https://www.baixuemianshi.com/zhuanti/sk_stl/")
					}
				})
				return
			}
			if(!that.userphone){
				uni.showToast({
					title: "请先登录",
					icon: "error",
					duration: 1000,
					complete(){
						that.toLogin()
					}
				})
				return
			}
			wx.requestSubscribeMessage({
				tmplIds: [that.templateId],
				success(res) {
					console.log(res);
					if (res[that.templateId] === 'accept') {
						db.collection('24sk_ggyy').add({
							data: {
								phone: that.userphone,
								name: that.name,
								status: 1, //发送状态 0表示已发送，1表示未发送
								time: new Date(),
								templateId: that.templateId // 模板ID
							},
							success(res) {
								uni.showToast({
									title:"您已预约",
									icon:"success",
									duration: 1000,
									complete(){
										that.changeWeb("https://www.baixuemianshi.com/zhuanti/sk_stl/")
									}
								})
							},
							fail(res) {
								console.log('存入数据库失败！');
							}
						});
						
					}else{
						wx.showToast({
							icon: 'none',
							title: '订阅提醒失败',
						})
						console.log(res[that.templateId], '失败')
					}
				},
				fail(req){
					console.log(req)
				}
			});
		},
		changeWeb(webInfo) {
			uni.navigateTo({
				url: '../../webview/index?href=' + encodeURIComponent(webInfo)
			});
		},
		changePath(path){
			uni.navigateTo({
				url: path
			})
		},
	},
	components: { }
};
</script>

<style lang="scss">
.poster {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100vh;
		z-index: 9999;
		background-size: 100%;
	}

	.poster .btn {
		position: absolute;
		bottom: 150rpx;
		left: 0;
		width: 100%;
	}
	.poster .btn image{display: block; width: 300rpx; margin: 0 auto; animation: pulse 1s linear infinite;}
	@keyframes pulse {
		0% {
		    -webkit-transform: scale3d(1, 1, 1);
		    transform: scale3d(1, 1, 1)
		  }
		  50% {
		    -webkit-transform: scale3d(1.05, 1.05, 1.05);
		    transform: scale3d(1.05, 1.05, 1.05)
		  }
		  100% {
		    -webkit-transform: scale3d(1, 1, 1);
		    transform: scale3d(1, 1, 1)
		  }
	}
	.name{position: fixed;bottom: 0; right: 0;  font-size: 18rpx; z-index: 10000;}
</style>
